package JPetStore.persistence.impl;

import JPetStore.domain.Account;
import JPetStore.domain.CartItem;
import JPetStore.domain.Item;
import JPetStore.persistence.CartDao;
import JPetStore.persistence.DBUtil;
import JPetStore.service.CatalogService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private static String IS_EXIST = "select * from cartmessage where username =?";
    private static String DELETE_ALL = "DELETE FROM cart WHERE USERNAME = ?";
    private static String ADD_CART = "insert into cartmessage(username, itemid) values(?,?)";
    private static final String INSERT_CART =
            "INSERT INTO cart (itemid,username,quantity) " +
                    "VALUES (?,?,?)";

    @Override
    public boolean is_exist(String username) {
        boolean is_exist = false;

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(IS_EXIST);
            pStatement.setString(1, username);
            ResultSet resultSet = pStatement.executeQuery();
            if (resultSet.next()) {
                is_exist = true;
            }

            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return is_exist;
    }

    @Override
    public int addCart(Account account, Item item) {
        int result = 0;

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(ADD_CART);
            pStatement.setString(1, account.getUsername());
            pStatement.setString(2, item.getItemId());
            result = pStatement.executeUpdate();
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return result;
    }

    @Override
    public int deleteCart(Account account, Item item) {
        int result = 0;

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement("delete from cartmessage where username =?");
            pStatement.setString(1, account.getUsername());
            result = pStatement.executeUpdate();
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }

    @Override
    public void getCart(Account loginAccount, List<CartItem> itemList) {
        try {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("SELECT * FROM cart WHERE USERNAME = ?");
            preparedStatement.setString(1, loginAccount.getUsername());
            resultSet = preparedStatement.executeQuery();
            itemList.clear();
            CatalogService catalogService=new CatalogService();
            while (resultSet.next()) {
                Item item=new Item();
                CartItem cartItem=new CartItem();
                item=catalogService.getItem(resultSet.getString("itemid"));
                cartItem.setInStock(true);
                cartItem.setItem(item);
                cartItem.setQuantity(resultSet.getInt("quantity"));
                itemList.add(cartItem);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateCart(Account loginAccount,CartItem cartItem){
       try{
           Connection connection = null;
           PreparedStatement preparedStatement = null;
           ResultSet resultSet = null;
           connection = DBUtil.getConnection();

           preparedStatement = connection.prepareStatement("DELETE FROM cart WHERE USERNAME = ? AND ITEMID = ?");
           preparedStatement.setString(1, loginAccount.getUsername());
           preparedStatement.setString(2, cartItem.getItem().getItemId());
           int rowsAffected=0;
           rowsAffected = preparedStatement.executeUpdate();
           if (rowsAffected > 0) {
               System.out.println("删除成功");
           }

          if(cartItem.getQuantity()!=0){
              preparedStatement = connection.prepareStatement(INSERT_CART);
              preparedStatement.setString(1,cartItem.getItem().getItemId());
              preparedStatement.setString(2,loginAccount.getUsername());
              preparedStatement.setInt(3,cartItem.getQuantity());
              rowsAffected = preparedStatement.executeUpdate();
              if (rowsAffected > 0) {
                  System.out.println("cart 插入成功");
              }
          }

           DBUtil.closePreparedStatement(preparedStatement);
           DBUtil.closeConnection(connection);
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    @Override
    public int deleteAll(Account loginAccount) {
        int rowsAffected=0;
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ALL);
            preparedStatement.setString(1, loginAccount.getUsername());
            rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("删除成功");
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rowsAffected;
    }

    @Override
    public String getItemIdByUsername(String username) {
        String id = null;

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement("select * from cartmessage where username =?");
            pStatement.setString(1, username);

            ResultSet resultSet;
            for(resultSet = pStatement.executeQuery(); resultSet.next(); id = resultSet.getString("itemId")) {
            }

            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return id;
    }

    @Override
    public List<Item> getItemIDListByUsername(String username) {
        CatalogService catalogService = new CatalogService();
        List<Item> itemList = new ArrayList();

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement("select * from cartmessage where username =?");
            pStatement.setString(1, username);
            ResultSet resultSet = pStatement.executeQuery();

            while(resultSet.next()) {
                new Item();
                Item item = catalogService.getItem(resultSet.getString("itemId"));
                itemList.add(item);
            }

            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return itemList;
    }

    @Override
    public void insertCart(List<CartItem> itemList, Account loginAccount) {
        try{
            Connection connection = DBUtil.getConnection();
            for(int i = 0;i < itemList.size();i++){
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CART);
                preparedStatement.setString(1,itemList.get(i).getItem().getItemId());
                preparedStatement.setString(2,loginAccount.getUsername());
                preparedStatement.setInt(3,itemList.get(i).getQuantity());
                int rowsAffected = preparedStatement.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("cart 插入成功");
                }
                DBUtil.closePreparedStatement(preparedStatement);
            }
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
